package com.lihepeng.leecode.juc;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 双重校验锁单例模式
 */
public class DoubleCheckLockSingleton {
    private static volatile DoubleCheckLockSingleton doubleCheckLockSingleton;
    private DoubleCheckLockSingleton(){
        System.out.println(Thread.currentThread().getName()+"构造方法被执行");
    }
    public static DoubleCheckLockSingleton getInstance(){
        if (doubleCheckLockSingleton ==null) {
            synchronized (DoubleCheckLockSingleton.class) {
                if (doubleCheckLockSingleton ==null) {
                    doubleCheckLockSingleton = new DoubleCheckLockSingleton();
                }
            }
        }
        return doubleCheckLockSingleton;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i=0;i<10;i++) {
            executorService.execute(() -> {
                DoubleCheckLockSingleton instance = DoubleCheckLockSingleton.getInstance();
            });
        }
        executorService.shutdown();

    }

}
